/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-07 13:44:19  
 * Copyright (c) 2015- joysuch-版权所有     
 */
package com.joysuch.wwyt.bp.controller;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;

import com.google.gson.Gson;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.bp.bean.BpEmergencyMaterialTypeEditBean;
import com.joysuch.wwyt.bp.bean.BpEmergencyMaterialTypeListBean;
import static com.joysuch.wwyt.bp.bean.BpEmergencyMaterialTypeListBean.Search;
import com.joysuch.wwyt.bp.entity.BpEmergencyMaterialType;
import com.joysuch.wwyt.bp.service.BpEmergencyMaterialTypeService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 应急消防管理——物资类型管理
 */
@RestController
@Api(tags = "应急消防管理—物资类型管理")
@RequestMapping("/wwyt/bp-emergency-material-type")
public class BpEmergencyMaterialTypeController {

    private static final Logger logger = LoggerFactory.getLogger(BpEmergencyMaterialTypeController.class);

    @Autowired
    private BpEmergencyMaterialTypeService bpEmergencyMaterialTypeService;

    /**
     * 新增物资类型
     * @param entity    待添加的物资类型信息[name]
     */
    @RequestMapping("add")
    public Object add(@Valid BpEmergencyMaterialTypeEditBean entity) {
        try {
            if (bpEmergencyMaterialTypeService.checkName(entity.getName())) {
                return ResultBean.fail(102, "物资类型名称重复");
            }
            bpEmergencyMaterialTypeService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    /**
     * 修改物资类型
     * @param entity    待修改的物资类型信息[id, name]
     */
    @RequestMapping("update")
    public Object update(@Valid BpEmergencyMaterialTypeEditBean entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            if (bpEmergencyMaterialTypeService.checkName(entity.getName())) {
                return ResultBean.fail(103, "物资类型名称重复");
            }
            BpEmergencyMaterialType temp = bpEmergencyMaterialTypeService.findById(entity.getId());
            if (temp != null && temp.getMaterialCount() > 0) {
                return ResultBean.fail(104, "该类型下存在物资，无法更新");
            }
            bpEmergencyMaterialTypeService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#"+entity.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    @RequestMapping("page")
    public Object page(Pageable page, Search condition) {
        try {
            Page<BpEmergencyMaterialTypeListBean> data = bpEmergencyMaterialTypeService.findByPage(page,condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    /**
     * 获取所有的物资类型列表
     */
    @RequestMapping("list")
    public Object listAll() {
        try {
            List<BpEmergencyMaterialTypeListBean> data = bpEmergencyMaterialTypeService.findAll();
            return ResultBean.wrapSuccessfulResult(data);
        } catch (Exception e) {
            logger.error("查询列表失败", e);
            return ResultBean.fail(101, "查询列表失败");
        }
    }

    /**
     * 删除物资类型
     * @param id    待删除的物资类型ID
     */
    @RequestMapping("delete")
    public Object delete(Long id) {
        try {
            BpEmergencyMaterialType temp = bpEmergencyMaterialTypeService.findById(id);
            if (temp != null && temp.getMaterialCount() > 0) {
                return ResultBean.fail(102, "该类型下存在物资，无法删除");
            }
            bpEmergencyMaterialTypeService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    /**
     * 批量操作物资类型
     * @param listJson  待操作的物资类型列表
     * @return
     */
    @RequestMapping("save")
    public Object save(String listJson) {
        try {
            if (listJson == null) {
                return ResultBean.fail(102, "未传入参数");
            }
            Gson gson = new Gson();
            List<Map<String, Object>> materialTypeList = gson.fromJson(listJson, ArrayList.class);
            bpEmergencyMaterialTypeService.save(materialTypeList);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("保存失败", e);
            return ResultBean.fail(101, "保存失败");
        }
    }
}
